package com.xcl.service.impl;

import com.xcl.mapper.XclLogisticsMapper;
import com.xcl.mapper.XclWaybillMapper;
import com.xcl.service.XclLogisticsServiceInter;
import com.zzg.entity.Logistics;
import com.zzg.entity.TmsRegion;
import com.zzg.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName XclLogisticsServiceImpl
 * @Description 物流承担服务层实现类
 * @Author: xiaochaolei
 * @date 2021/7/9 9:45
 * @Version 1. 0
 */
@Service
public class XclLogisticsServiceImpl implements XclLogisticsServiceInter {
    @Autowired
    XclLogisticsMapper xclLogisticsMapper;
    @Autowired
    XclWaybillMapper xclWaybillMapper;

    @Override
    public List<Logistics> getAllLogistics() {
        return xclLogisticsMapper.getAllLogistics();
    }

    @Override
    public List<Logistics> getAllLogisticsByLimit(int page, int limit) {
        return xclLogisticsMapper.getAllLogisticsByLimit((page-1)*limit,limit);
    }

    @Override
    public int insertLogistics(Logistics logistics,String tms_region) {
        //根据字符串分割
        String[] split = tms_region.split("-");
        //用于存储地区id
        String regionId="";
        //判断是否存在
        TmsRegion existConsignerArea = xclWaybillMapper.isExistConsignerArea(split[0]+split[1]+split[2]+split[3]);
        if (existConsignerArea==null){
            //实例化地区(做新增操作)
            TmsRegion tmsRegion=new TmsRegion();
            //设置id
            tmsRegion.setRegionId(UuidUtil.getUuid());
            //设置省
            tmsRegion.setProvince(split[0]);
            //设置市
            tmsRegion.setCity(split[1]);
            //设置区
            tmsRegion.setDistrict(split[2]);
            //设置详细地址
            tmsRegion.setDetailedAddress(split[0]+split[1]+split[2]+ split[3]);
            //得到id
            regionId=tmsRegion.getRegionId();
            //新增地区
            xclWaybillMapper.insertTmsRegion(tmsRegion);
        }else {
            //得到id
            regionId=existConsignerArea.getRegionId();
        }
        //设置地区id
        logistics.setRegionId(regionId);
        //设置承担物流商id
        logistics.setLogisticsId(UuidUtil.getUuid());
        //设置备注地址
        logistics.setRemark(split[0]+split[1]+split[2]+ split[3]);
        return xclLogisticsMapper.insertLogistics(logistics);
    }

    @Override
    public Logistics getLogisticsById(String id) {
        return xclLogisticsMapper.getLogisticsById(id);
    }

    @Override
    public int updateLogistics(Logistics logistics,String tms_region) {
        //根据字符串分割
        String[] split = tms_region.split("-");
        //用于存储地区id
        String regionId="";

        //判断数据回填的时候是否更改了
        if (split.length==1){
            //判断是否存在
            TmsRegion existConsignerArea = xclWaybillMapper.isExistConsignerArea(tms_region);
            //得到id
            regionId=existConsignerArea.getRegionId();
            //设置备注地址
            logistics.setRemark(tms_region);
        }else {
            //判断是否存在
            TmsRegion existConsignerArea = xclWaybillMapper.isExistConsignerArea(split[0]+split[1]+split[2]+split[3]);
            if (existConsignerArea==null){
                //实例化地区(做新增操作)
                TmsRegion tmsRegion=new TmsRegion();
                //设置id
                tmsRegion.setRegionId(UuidUtil.getUuid());
                //设置省
                tmsRegion.setProvince(split[0]);
                //设置市
                tmsRegion.setCity(split[1]);
                //设置区
                tmsRegion.setDistrict(split[2]);
                //设置详细地址
                tmsRegion.setDetailedAddress(split[0]+split[1]+split[2]+ split[3]);
                //得到id
                regionId=tmsRegion.getRegionId();
                //新增地区
                xclWaybillMapper.insertTmsRegion(tmsRegion);
            }else {
                //得到id
                regionId=existConsignerArea.getRegionId();
            }
            //设置地区id
            logistics.setRegionId(regionId);
            //设置备注地址
            logistics.setRemark(split[0]+split[1]+split[2]+ split[3]);
        }
        int i=xclLogisticsMapper.updateLogistics(logistics);
        return i;
    }

    @Override
    public List<Logistics> getAllLogisticsByForwardingUnit(String forwardingUnit, int page, int limit) {
        return xclLogisticsMapper.getAllLogisticsByForwardingUnit(forwardingUnit,(page-1)*limit,limit);
    }
}
